package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.model.Checkadmin;
import cn.edu.nuc.onlinestore.model.Checked;
import cn.edu.nuc.onlinestore.model.Checkuser;
import cn.edu.nuc.onlinestore.util.Acceptutil;
import cn.edu.nuc.onlinestore.vo.Person;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class AdminLogin extends JFrame {
	static AdminStore add;
	private JPanel contentPane;
	private JTextField adminname;
	private JPasswordField adminpassword;
	static JFrame frame1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame1 = new AdminLogin();
					frame1.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminLogin() {
		
		File sele_2=new File("D:/store/admin/");
		File sele_3=new File("D:/store/user/");
		File sele_4=new File("D:/store/Online/user/");
		File sele_5=new File("D:/store/goods/");
		File sele_6=new File("D:/store/Online/admin/");
		
		File[] sele_1=new File[]{sele_2,sele_3,sele_4,sele_5,sele_6};
		System.out.println(sele_1.length);
		for(int i=0;i<sele_1.length;i++){
			if(!sele_1[i].exists()){
				sele_1[i].mkdirs();
			}
		}
		
		
		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名:");
		label.setForeground(new Color(192, 192, 192));
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码:");
		label_1.setForeground(new Color(192, 192, 192));
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);
		
		adminname = new JTextField();
		adminname.setBounds(151, 146, 197, 21);
		contentPane.add(adminname);
		adminname.setColumns(10);
		
		adminpassword = new JPasswordField();
		adminpassword.setBounds(151, 185, 197, 21);
		contentPane.add(adminpassword);
		
		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username=adminname.getText();
				String password=adminpassword.getText();
				boolean t = false;
				Checked ck=new Checkadmin();
				try {
					try {
						t=ck.checkuser(username, password);
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(t){
					FileOutputStream out;
					ObjectOutputStream dout;
					try {
						out = new FileOutputStream("D:/store/Online/admin/"+username+".db");
						dout=new ObjectOutputStream(out);
						dout.writeObject(new Person(username,password));
						
						out.close();
						dout.close();
						
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					
					
					frame1.dispose();
					add = new AdminStore(username);
					add.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					add.setVisible(true);
					Acceptutil ac=new Acceptutil();
					ac.start();
					
				}else{
					JOptionPane.showMessageDialog(null, "用户名或密码错误", "alert", JOptionPane.ERROR_MESSAGE); 
				}
			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(AdminLogin.class.getResource("/image/a.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Checked ck=new Checkadmin();
				try {
					System.out.println(ck.adduser("wb", "123"));
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(70, 234, 93, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setIcon(new ImageIcon(AdminLogin.class.getResource("/image/3cx7g4fbtt.jpg")));
		lblNewLabel_1.setBounds(0, 0, 445, 311);
		contentPane.add(lblNewLabel_1);
	}
}
